package com.kinghunt.misp.service.impl.roomx;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.roomx.RoomDTO;
import com.kinghunt.misp.common.param.roomx.RoomParam;
import com.kinghunt.misp.dal.dao.roomx.RoomDAO;
import com.kinghunt.misp.dal.model.custx.CustDO;
import com.kinghunt.misp.dal.model.roomx.RoomDO;
import com.kinghunt.misp.service.api.roomx.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 房间管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class RoomServiceImpl implements RoomService {

    @Autowired(required = false)
    private RoomDAO roomDAO;

    @Override
    public ApiResult<PageInfo<RoomDTO>> getRoomList(RoomParam roomParam) {
        int offset = (roomParam.getPageNum() - 1) * roomParam.getPageSize();
        RoomDO roomDO = ConvertUtil.convert(roomParam, RoomDO.class);
        int count = roomDAO.selectRoomListCount(roomDO);
        PageInfo<RoomDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(roomParam.getPageNum());
        pageInfo.setPageSize(roomParam.getPageSize());
        List<RoomDO> roomDOList = roomDAO.selectRoomList(roomDO, offset, roomParam.getPageSize());
        List<RoomDTO> dto =  ConvertUtil.convert(roomDOList, RoomDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<RoomDTO> getRoomDetail(Long roomId) {
        RoomDO RoomDO = roomDAO.selectByPrimaryKey(roomId);
        RoomDTO RoomDTO = ConvertUtil.convert(RoomDO, RoomDTO.class);
        return ApiResult.ok(RoomDTO);
    }

    @Override
    public ApiResult addRoom(RoomParam roomParam) {
        //判断房间号是否唯一
        List<RoomDO> roomDOList = roomDAO.selectByRoomNo(roomParam.getRoomNo());
        if(roomDOList != null && roomDOList.size() > 0){
            return ApiResult.failOfMessage("您新增的房间编号已存在！ ");
        }
        RoomDO RoomDO = ConvertUtil.convert(roomParam, RoomDO.class);
        // 保存地址到数据库
        roomDAO.insert(RoomDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateRoom(RoomParam roomParam) {
        RoomDO RoomDO = roomDAO.selectByPrimaryKey(roomParam.getRoomId());
        if(null == RoomDO){
            return ApiResult.failOfMessage("您修改的房间不存在！ ");
        }
        // 修改当前地址
        RoomDO updateRoomDO = ConvertUtil.convert(roomParam, RoomDO.class);
        roomDAO.updateByPrimaryKeySelective(updateRoomDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteRoom(Long roomId) {
        RoomDO RoomDO = roomDAO.selectByPrimaryKey(roomId);
        if(null == RoomDO){
            return ApiResult.failOfMessage("您删除的房间不存在！ ");
        }
        roomDAO.deleteByPrimaryKey(roomId);
        return ApiResult.ok();
    }
}
